/**
 * Copyright 2011 Powersi. All rights reserved.
 * 
 */
package com.powersi.sys.manager.action;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.powersi.hygeia.framework.GlobeContext;
import com.powersi.hygeia.web.BaseAction;
import com.powersi.sys.entity.DownloadFile;

/**
 * The Class QuerySystemLogAction.
 */
public class QuerySystemLogAction extends BaseAction {

	/** The Constant serialVersionUID. */
	private static final long serialVersionUID = 1L;

	/** The download file. */
	DownloadFile downloadFile;

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.opensymphony.xwork2.ActionSupport#execute()
	 */
	@Override
	public String execute() throws Exception {

		try {
			List lst = new ArrayList();
			String logPath = GlobeContext.getPhysicalPath("WEB-INF/logs");

			File root = new File(logPath);
			if (root.exists() && root.isDirectory()) {
				File[] files = root.listFiles();
				for (File file : files) {
					if (file.isFile()) {
						Map map = new HashMap();
						map.put("file_name", file.getName());
						map.put("file_length", Long.valueOf(file.length()));
						map.put("last_modified",
								new java.util.Date(file.lastModified()));

						lst.add(map);
					}
				}
			}

			setAttribute("info", lst);
			return SUCCESS;
		} catch (Exception ex) {
			saveError(ex);
			return ERROR;
		}

	}

	/**
	 * View.
	 * 
	 * @return the string
	 */
	public String view() {
		try {
			String logPath = GlobeContext.getPhysicalPath("WEB-INF/logs/"
					+ getParameter("fileName"));
			downloadFile = new DownloadFile(logPath);
			return SUCCESS;
		} catch (Exception ex) {
			saveError(ex);
			return ERROR;
		}
	}

	/**
	 * Gets the download file.
	 * 
	 * @return the download file
	 */
	public DownloadFile getDownloadFile() {
		return downloadFile;
	}

	/**
	 * Sets the download file.
	 * 
	 * @param downloadFile
	 *            the new download file
	 */
	public void setDownloadFile(DownloadFile downloadFile) {
		this.downloadFile = downloadFile;
	}
}